package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Override
    public PageResponse<NursingPlanVo> pageNursingPlan(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> planPageResponse = nursingPlanMapper.pageNursingPlan(name, status);
        return PageResponse.of(planPageResponse, NursingPlanVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setStatus(1);
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        if (projectPlans != null && !projectPlans.isEmpty()) {
            List<NursingProjectPlan> ppList = projectPlans.stream().map(pp -> BeanUtil.toBean(pp, NursingProjectPlan.class)).collect(Collectors.toList());
            ppList.forEach(c -> c.setPlanId(nursingPlan.getId()));
            nursingProjectPlanMapper.insertNursingProjectPlan(ppList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public NursingPlanVo getNursingPlan(Integer id) {
        NursingPlan nursingPlan = nursingPlanMapper.getNursingPlanById(id);
        Long planId = nursingPlan.getId();
        List<NursingProjectPlanVo> nursingProjectPlans = nursingProjectPlanMapper.getNursingProjectPlanByPlanId(planId);
        log.info("-----------nursingProjectPlans-----------{}", nursingProjectPlans);
        nursingPlan.setProjectPlans(nursingProjectPlans);
        return BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlanMapper.updateNursingPlan(nursingPlan);
        nursingProjectPlanMapper.deleteNursingProjectPlanByPlanId(nursingPlanDto.getId());
        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        if (projectPlans != null && !projectPlans.isEmpty()) {
            List<NursingProjectPlan> nppList = projectPlans.stream().map(pp -> BeanUtil.toBean(pp, NursingProjectPlan.class)).collect(Collectors.toList());
            nppList.forEach(npp -> npp.setPlanId(nursingPlan.getId()));
            nursingProjectPlanMapper.insertNursingProjectPlan(nppList);
        }
    }

    @Override
    public void deleteNursingPlan(Integer id) {
        Integer count = nursingPlanMapper.getCount(id);
        if (count != null && count > 0) {
            throw new BaseException(BasicEnum.NURSING_PLAN_DELETE);
        }
        nursingPlanMapper.deleteNursingPlanById(id);
        nursingProjectPlanMapper.deleteNursingProjectPlanByPlanId(Long.valueOf(id));
    }

    @Override
    public void forbiddenNursingPlan(Integer id, Integer status) {
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setId(Long.valueOf(id));
        nursingPlan.setStatus(status);
        nursingPlanMapper.updateNursingPlan(nursingPlan);
    }

    @Override
    public List<NursingPlanVo> getAllNursingPlan() {

        return nursingPlanMapper.selectNursingPlan();
    }
}

